﻿using HashidsNet;

namespace CleanArchitecture.Core.Helper
{
    public sealed class HashidsHelper
    {
        private static Hashids _cachedhashids;
        public static string Encode(string salt, int number)
        {
            if (_cachedhashids == null)
                _cachedhashids = new Hashids(salt, 8);
            return _cachedhashids.Encode(number);
        }

        public static int Decode(string salt, string hash)
        {
            if (_cachedhashids == null)
                _cachedhashids = new Hashids(salt, 8);
            int[] numbers = Decode(_cachedhashids, hash, 1);
            return numbers == null ? 0 : numbers[0];
        }

        private static int[] Decode(Hashids hashobj, string hash, int length)
        {
            int[] numbers = hashobj.Decode(hash);
            return numbers.Length == length ? numbers : null;
        }
    }
}
